package com.player.entity;

import java.util.Date;

import javax.persistence.Embedded;
import javax.persistence.Id;
import javax.persistence.PrePersist;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Indexed;
import com.player.util.PlayerDateFormatter;

@Entity
public class Game {

	@Id
	Long id;

	@Indexed
	Long creatorId;

	@Indexed
	Long opponentId;

	@Indexed
	Date dateCreated;

	@Embedded
	GameResult result;

	public Game() {
	}

	public Game(Long creatorId, Long opponentId) {
		super();
		this.creatorId = creatorId;
		this.opponentId = opponentId;
	}

	@PrePersist
	public void prePersist() {
		dateCreated = new Date();
	}

	public Long getId() {
		return id;
	}

	public Long getCreatorId() {
		return creatorId;
	}

	public Long getOpponentId() {
		return opponentId;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public boolean hasResult() {
		return result != null;
	}

	public GameResult getResult() {
		return result;
	}

	public void setResult(GameResult result) {
		this.result = result;
	}

	public void confirmResult() {
		if (hasResult()) {
			result.confirm();
		}
	}

	@Override
	public String toString() {
		return "Game [id=" + id + ", creatorId=" + creatorId + ", opponentId=" + opponentId + ", dateCreated="
				+ PlayerDateFormatter.dateToString(dateCreated) + "]";
	}

}
